#!/usr/bin/perl

# Copyright (c) 2006 Red Hat, Inc. All rights reserved. This copyrighted material 
# is made available to anyone wishing to use, modify, copy, or
# redistribute it subject to the terms and conditions of the GNU General
# Public License v.2.
#
# This program is distributed in the hope that it will be useful, but WITHOUT ANY
# WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A
# PARTICULAR PURPOSE. See the GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program; if not, write to the Free Software
# Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
#
# Author: Petr Splichal <psplicha@redhat.com>

# This perl script takes all supplied files or stdin and extracts
# every =pod section that does not include =head1 headings except
# those which contain beakerlib-manual-{header,footer} directives.
#
# Finally it spits out parsed =pod sections in the following order:
# beakerlib-manual-header
# beakerlib-manual-include (all non-=head1 sections included here)
# beakerlib-manual-footer

my $pod = 0;
my $type = '';
my %text = (include => '=head1 FUNCTIONS');

while (<>) {
    # end of =pod section
    if (m/^=cut/) {
        $pod = 0;
    }

    # recognize header/footer and trash =head1's
    $type = 'header' if m/beakerlib-manual-header/;
    $type = 'footer' if /beakerlib-manual-footer/;
    $type = 'trash'  if /^=head1/ && ! ($type =~ m/header|footer/);

    # add to corresponding place
    $text{$type} .= $_ if $pod;

    # start of =pod section
    if (m/^=pod/) {
        $pod = 1;
        $type = 'include';
    }
}

print $text{header};
print $text{include};
print $text{footer};
